home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / phpMyAdmin / libraries / mysql_charsets.lib.php < prev    next >
PHP Script  |  2005-05-05  |  15KB  |  360 lines

  1. <?php
  2. /* $Id: mysql_charsets.lib.php,v 2.28.2.1 2005/05/05 16:29:28 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. if (PMA_MYSQL_INT_VERSION >= 40100){
  6.  
  7.     $res = PMA_DBI_query('SHOW CHARACTER SET;');
  8.  
  9.     $mysql_charsets = array();
  10.     while ($row = PMA_DBI_fetch_assoc($res)) {
  11.         $mysql_charsets[] = $row['Charset'];
  12.         $mysql_charsets_maxlen[$row['Charset']] = $row['Maxlen'];
  13.         $mysql_charsets_descriptions[$row['Charset']] = $row['Description'];
  14.     }
  15.     @PMA_DBI_free_result($res);
  16.     unset($res, $row);
  17.  
  18.     $res = PMA_DBI_query('SHOW COLLATION;');
  19.  
  20.     $mysql_charsets_count = count($mysql_charsets);
  21.     sort($mysql_charsets, SORT_STRING);
  22.  
  23.     $mysql_collations = array_flip($mysql_charsets);
  24.     $mysql_default_collations = $mysql_collations_flat = $mysql_charsets_available = $mysql_collations_available = array();
  25.     while ($row = PMA_DBI_fetch_assoc($res)) {
  26.         if (!is_array($mysql_collations[$row['Charset']])) {
  27.             $mysql_collations[$row['Charset']] = array($row['Collation']);
  28.         } else {
  29.             $mysql_collations[$row['Charset']][] = $row['Collation'];
  30.         }
  31.         $mysql_collations_flat[] = $row['Collation'];
  32.         if ((isset($row['D']) && $row['D'] == 'Y') || (isset($row['Default']) && $row['Default'] == 'Yes')) {
  33.             $mysql_default_collations[$row['Charset']] = $row['Collation'];
  34.         }
  35.         //$mysql_collations_available[$row['Collation']] = !isset($row['Compiled']) || $row['Compiled'] == 'Yes';
  36.         $mysql_collations_available[$row['Collation']] = TRUE;
  37.         $mysql_charsets_available[$row['Charset']] = !empty($mysql_charsets_available[$row['Charset']]) || !empty($mysql_collations_available[$row['Collation']]);
  38.     }
  39.  
  40.     $mysql_collations_count = count($mysql_collations_flat);
  41.     sort($mysql_collations_flat, SORT_STRING);
  42.     foreach ($mysql_collations AS $key => $value) {
  43.         sort($mysql_collations[$key], SORT_STRING);
  44.         reset($mysql_collations[$key]);
  45.     }
  46.  
  47.     @PMA_DBI_free_result($res);
  48.     unset($res, $row);
  49.  
  50.     function PMA_getCollationDescr($collation) {
  51.         static $collation_cache;
  52.  
  53.         if (!is_array($collation_cache)) {
  54.             $collation_cache = array();
  55.         } elseif (isset($collation_cache[$collation])) {
  56.             return $collation_cache[$collation];
  57.         }
  58.  
  59.         if ($collation == 'binary') {
  60.             return $GLOBALS['strBinary'];
  61.         }
  62.         $parts = explode('_', $collation);
  63.         if (count($parts) == 1) {
  64.             $parts[1] = 'general';
  65.         } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
  66.             $parts[2] = $parts[1];
  67.             $parts[1] = 'general';
  68.         }
  69.         $descr = '';
  70.         switch ($parts[1]) {
  71.             case 'bulgarian':
  72.                 $descr = $GLOBALS['strBulgarian'];
  73.                 break;
  74.             case 'chinese':
  75.                 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
  76.                     $descr = $GLOBALS['strSimplifiedChinese'];
  77.                 } elseif ($parts[0] == 'big5') {
  78.                     $descr = $GLOBALS['strTraditionalChinese'];
  79.                 }
  80.                 break;
  81.             case 'ci':
  82.                 $descr = $GLOBALS['strCaseInsensitive'];
  83.                 break;
  84.             case 'cs':
  85.                 $descr = $GLOBALS['strCaseSensitive'];
  86.                 break;
  87.             case 'croatian':
  88.                 $descr = $GLOBALS['strCroatian'];
  89.                 break;
  90.             case 'czech':
  91.                 $descr = $GLOBALS['strCzech'];
  92.                 break;
  93.             case 'danish':
  94.                 $descr = $GLOBALS['strDanish'];
  95.                 break;
  96.             case 'english':
  97.                 $descr = $GLOBALS['strEnglish'];
  98.                 break;
  99.             case 'estonian':
  100.                 $descr = $GLOBALS['strEstonian'];
  101.                 break;
  102.             case 'german1':
  103.                 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strDictionary'] . ')';
  104.                 break;
  105.             case 'german2':
  106.                 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strPhoneBook'] . ')';
  107.                 break;
  108.             case 'hungarian':
  109.                 $descr = $GLOBALS['strHungarian'];
  110.                 break;
  111.             case 'icelandic':
  112.                 $descr = $GLOBALS['strIcelandic'];
  113.                 break;
  114.             case 'japanese':
  115.                 $descr = $GLOBALS['strJapanese'];
  116.                 break;
  117.             case 'latvian':
  118.                 $descr = $GLOBALS['strLatvian'];
  119.                 break;
  120.             case 'lithuanian':
  121.                 $descr = $GLOBALS['strLithuanian'];
  122.                 break;
  123.             case 'korean':
  124.                 $descr = $GLOBALS['strKorean'];
  125.                 break;
  126.             case 'persian':
  127.                 $descr = $GLOBALS['strPersian'];
  128.                 break;
  129.             case 'polish':
  130.                 $descr = $GLOBALS['strPolish'];
  131.                 break;
  132.             case 'roman':
  133.                 $descr = $GLOBALS['strWestEuropean'];
  134.                 break;
  135.             case 'romanian':
  136.                 $descr = $GLOBALS['strRomanian'];
  137.                 break;
  138.             case 'slovak':
  139.                 $descr = $GLOBALS['strSlovak'];
  140.                 break;
  141.             case 'slovenian':
  142.                 $descr = $GLOBALS['strSlovenian'];
  143.                 break;
  144.             case 'spanish':
  145.                 $descr = $GLOBALS['strSpanish'];
  146.                 break;
  147.             case 'spanish2':
  148.                 $descr = $GLOBALS['strTraditionalSpanish'];
  149.                 break;
  150.             case 'swedish':
  151.                 $descr = $GLOBALS['strSwedish'];
  152.                 break;
  153.             case 'thai':
  154.                 $descr = $GLOBALS['strThai'];
  155.                 break;
  156.             case 'turkish':
  157.                 $descr = $GLOBALS['strTurkish'];
  158.                 break;
  159.             case 'ukrainian':
  160.                 $descr = $GLOBALS['strUkrainian'];
  161.                 break;
  162.             case 'unicode':
  163.                 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  164.                 break;
  165.             case 'bin':
  166.                 $is_bin = TRUE;
  167.             case 'general':
  168.                 switch ($parts[0]) {
  169.                     // Unicode charsets
  170.                     case 'ucs2':
  171.                     case 'utf8':
  172.                         $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  173.                         break;
  174.                     // West European charsets
  175.                     case 'ascii':
  176.                     case 'cp850':
  177.                     case 'dec8':
  178.                     case 'hp8':
  179.                     case 'latin1':
  180.                     case 'macroman':
  181.                         $descr = $GLOBALS['strWestEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  182.                         break;
  183.                     // Central European charsets
  184.                     case 'cp1250':
  185.                     case 'cp852':
  186.                     case 'latin2':
  187.                     case 'macce':
  188.                         $descr = $GLOBALS['strCentralEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  189.                         break;
  190.                     // Russian charsets
  191.                     case 'cp866':
  192.                     case 'koi8r':
  193.                         $descr = $GLOBALS['strRussian'];
  194.                         break;
  195.                     // Simplified Chinese charsets
  196.                     case 'gb2312':
  197.                     case 'gbk':
  198.                         $descr = $GLOBALS['strSimplifiedChinese'];
  199.                         break;
  200.                     // Japanese charsets
  201.                     case 'sjis':
  202.                     case 'ujis':
  203.                     case 'cp932':
  204.                     case 'eucjpms':
  205.                         $descr = $GLOBALS['strJapanese'];
  206.                         break;
  207.                     // Baltic charsets
  208.                     case 'cp1257':
  209.                     case 'latin7':
  210.                         $descr = $GLOBALS['strBaltic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  211.                         break;
  212.                     // Other
  213.                     case 'armscii8':
  214.                     case 'armscii':
  215.                         $descr = $GLOBALS['strArmenian'];
  216.                         break;
  217.                     case 'big5':
  218.                         $descr = $GLOBALS['strTraditionalChinese'];
  219.                         break;
  220.                     case 'cp1251':
  221.                         $descr = $GLOBALS['strCyrillic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  222.                         break;
  223.                     case 'cp1256':
  224.                         $descr = $GLOBALS['strArabic'];
  225.                         break;
  226.                     case 'euckr':
  227.                         $descr = $GLOBALS['strKorean'];
  228.                         break;
  229.                     case 'hebrew':
  230.                         $descr = $GLOBALS['strHebrew'];
  231.                         break;
  232.                     case 'geostd8':
  233.                         $descr = $GLOBALS['strGeorgian'];
  234.                         break;
  235.                     case 'greek':
  236.                         $descr = $GLOBALS['strGreek'];
  237.                         break;
  238.                     case 'keybcs2':
  239.                         $descr = $GLOBALS['strCzechSlovak'];
  240.                         break;
  241.                     case 'koi8u':
  242.                         $descr = $GLOBALS['strUkrainian'];
  243.                         break;
  244.                     case 'latin5':
  245.                         $descr = $GLOBALS['strTurkish'];
  246.                         break;
  247.                     case 'swe7':
  248.                         $descr = $GLOBALS['strSwedish'];
  249.                         break;
  250.                     case 'tis620':
  251.                         $descr = $GLOBALS['strThai'];
  252.                         break;
  253.                     default:
  254.                         $descr = $GLOBALS['strUnknown'];
  255.                         break;
  256.                 }
  257.                 if (!empty($is_bin)) {
  258.                     $descr .= ', ' . $GLOBALS['strBinary'];
  259.                 }
  260.                 break;
  261.             default: $descr = $GLOBALS['strUnknown'];
  262.         }
  263.         if (!empty($parts[2])) {
  264.             if ($parts[2] == 'ci') {
  265.                 $descr .= ', ' . $GLOBALS['strCaseInsensitive'];
  266.             } elseif ($parts[2] == 'cs') {
  267.                 $descr .= ', ' . $GLOBALS['strCaseSensitive'];
  268.             }
  269.         }
  270.  
  271.         $collation_cache[$collation] = $descr;
  272.         return $descr;
  273.     }
  274.  
  275.     function PMA_getDbCollation($db) {
  276.         global $userlink;
  277.         if (PMA_MYSQL_INT_VERSION >= 50000 && $db == 'information_schema') {
  278.             // We don't have to check the collation of the virtual
  279.             // information_schema database: We know it!
  280.             return 'utf8_general_ci';
  281.         }
  282.         if (PMA_MYSQL_INT_VERSION >= 40101) {
  283.             // MySQL 4.1.0 does not support seperate charset settings
  284.             // for databases.
  285.             $res = PMA_DBI_query('SHOW CREATE DATABASE ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE);
  286.             $row = PMA_DBI_fetch_row($res);
  287.             PMA_DBI_free_result($res);
  288.             $tokenized = explode(' ', $row[1]);
  289.             unset($row, $res, $sql_query);
  290.  
  291.             for ($i = 1; $i + 3 < count($tokenized); $i++) {
  292.                 if ($tokenized[$i] == 'DEFAULT' && $tokenized[$i + 1] == 'CHARACTER' && $tokenized[$i + 2] == 'SET') {
  293.                     // We've found the character set!
  294.                     if (isset($tokenized[$i + 5]) && $tokenized[$i + 4] == 'COLLATE') {
  295.                         return $tokenized[$i + 5]; // We found the collation!
  296.                     } else {
  297.                         // We did not find the collation, so let's return the
  298.                         // default collation for the charset we've found.
  299.                         return $GLOBALS['mysql_default_collations'][$tokenized [$i + 3]];
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.         return '';
  305.     }
  306.  
  307.     define('PMA_CSDROPDOWN_COLLATION', 0);
  308.     define('PMA_CSDROPDOWN_CHARSET',   1);
  309.  
  310.     function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION, $name = NULL, $id = NULL, $default = NULL, $label = TRUE, $indent = 0, $submitOnChange = FALSE, $displayUnavailable = FALSE) {
  311.         global $mysql_charsets, $mysql_charsets_descriptions, $mysql_charsets_available, $mysql_collations, $mysql_collations_available;
  312.  
  313.         if (empty($name)) {
  314.             if ($type == PMA_CSDROPDOWN_COLLATION) {
  315.                 $name = 'collation';
  316.             } else {
  317.                 $name = 'character_set';
  318.             }
  319.         }
  320.  
  321.         $spacer = '';
  322.         for ($i = 1; $i <= $indent; $i++) $spacer .= '    ';
  323.  
  324.         $return_str  = $spacer . '<select name="' . htmlspecialchars($name) . '"' . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"') . ($submitOnChange ? ' onchange="this.form.submit();"' : '') . '>' . "\n";
  325.         if ($label) {
  326.             $return_str .= $spacer . '    <option value="">' . ($type == PMA_CSDROPDOWN_COLLATION ? $GLOBALS['strCollation'] : $GLOBALS['strCharset']) . '</option>' . "\n";
  327.         }
  328.         $return_str .= $spacer . '    <option value=""></option>' . "\n";
  329.         foreach ($mysql_charsets as $current_charset) {
  330.             if (!$mysql_charsets_available[$current_charset]) {
  331.                 continue;
  332.             }
  333.             $current_cs_descr = empty($mysql_charsets_descriptions[$current_charset]) ? $current_charset : $mysql_charsets_descriptions[$current_charset];
  334.             if ($type == PMA_CSDROPDOWN_COLLATION) {
  335.                 $return_str .= $spacer . '    <optgroup label="' . $current_charset . '" title="' . $current_cs_descr . '">' . "\n";
  336.                 foreach ($mysql_collations[$current_charset] as $current_collation) {
  337.                     if (!$mysql_collations_available[$current_collation]) {
  338.                         continue;
  339.                     }
  340.                     $return_str .= $spacer . '        <option value="' . $current_collation . '" title="' . PMA_getCollationDescr($current_collation) . '"' . ($default == $current_collation ? ' selected="selected"' : '') . '>' . $current_collation . '</option>' . "\n";
  341.                 }
  342.                 $return_str .= $spacer . '    </optgroup>' . "\n";
  343.             } else {
  344.                 $return_str .= $spacer . '    <option value="' . $current_charset . '" title="' . $current_cs_descr . '"' . ($default == $current_charset ? ' selected="selected"' : '') . '>' . $current_charset . '</option>' . "\n";
  345.             }
  346.         }
  347.         $return_str .= $spacer . '</select>' . "\n";
  348.  
  349.         return $return_str;
  350.     }
  351.  
  352.     function PMA_generateCharsetQueryPart($collation) {
  353.         list($charset) = explode('_', $collation);
  354.         return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
  355.     }
  356.  
  357. }
  358.  
  359. ?>
  360.